package com.chuangyang.fixboxmaster.ui.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.chuangyang.fixboxmaster.AppData;
import com.chuangyang.fixboxmaster.R;
import com.chuangyang.fixboxmaster.bean.B2BPart;
import com.chuangyang.fixboxmaster.listener.B2BPartNumChangeListener;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by vanshu on 15/11/11 10:59.
 */
public class GetPartAdapter extends BaseAdapter {
    private List<B2BPart> mb2bPart = new ArrayList<>();
    private B2BPartNumChangeListener listener;

    public GetPartAdapter(List<B2BPart> parts){
        this.mb2bPart=parts;
    }
    public void setNumberChangeListener(B2BPartNumChangeListener listener){
        this.listener = listener;
    }

    @Override
    public int getCount() {
        return mb2bPart == null? 0:mb2bPart.size();
    }

    @Override
    public B2BPart getItem(int position) {
        return mb2bPart == null? null:mb2bPart.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        final ViewHolder holder;
        if (convertView == null)
        {
            holder = new ViewHolder();
            convertView = LayoutInflater.from(AppData.getContext()).inflate(R.layout.select_part_list_item, parent, false);
            holder.mTitle = (TextView)convertView.findViewById(R.id.part_title);
            holder.mNumControlLayout = (LinearLayout)convertView.findViewById(R.id.part_num_control);
            holder.mPartNumView = (TextView)convertView.findViewById(R.id.num);
            holder.mNumPlusView = (LinearLayout)convertView.findViewById(R.id.num_plus);
            holder.mNumReduceView = (LinearLayout)convertView.findViewById(R.id.num_reduce);
            holder.mPrice = (TextView)convertView.findViewById(R.id.part_price);
            convertView.setTag(holder);
        }
        else
        {
            holder = (ViewHolder)convertView.getTag();
        }
        final B2BPart part = getItem(position);
        holder.mNumPlusView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                part.num++;
                holder.mPartNumView.setText("" + part.num);
                listener.onChange(part);
            }
        });

        holder.mNumReduceView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                part.num--;
                if (part.num <= 0)
                    part.num = 0;
                holder.mPartNumView.setText("" + part.num);
                listener.onChange(part);
            }
        });
        if (part != null)
        {
            holder.mNumControlLayout.setVisibility(View.VISIBLE);
            holder.mPartNumView.setText(""+part.num);
            holder.mTitle.setText(part.PARTS_NAME);
            holder.mPrice.setText("￥"+part.PARTS_PRICE);
        }
        return convertView;
    }


    public static class ViewHolder{
        public TextView mTitle;
        public LinearLayout mNumControlLayout;
        public LinearLayout mNumPlusView;
        public TextView mPartNumView;
        public LinearLayout mNumReduceView;
        public TextView mPrice;
    }
}
